﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.Data;
using System.Data;

namespace blackhouse.BlackApart.Data.Sql {
    public class ActionDataAccess : AdoActionDataAccess {

        #region Fields

        private string connectionString;
        private MsSqlAdoDataHelper dataHelper;

        #endregion

        #region Constructors

        public ActionDataAccess(string sqlConnectionString) {
            this.connectionString = sqlConnectionString;
            this.dataHelper = new MsSqlAdoDataHelper(this.connectionString);
            SqlVersioning versioning = new SqlVersioning(this.connectionString, this.GetType().Assembly);
        }

        #endregion

        #region Methods

        public override void PushMailSended(int id) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_ActionPushMailMarkAsSended";
            this.dataHelper.AddParameter("Id", id, DbType.Int32);
            this.dataHelper.ExecuteWithoutResult();
        }

        protected override IDataReader GetReaderForPushMailAction(int id) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_ActionPushMailGet";
            this.dataHelper.AddParameter("Id", id, DbType.Int32);
            return this.dataHelper.Execute();
        }

        public override void SavePushMail(string content, bool isHtml, string title, string toMails) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_ActionPushMailSave";
            this.dataHelper.AddParameter("Content", content);
            this.dataHelper.AddParameter("IsHtml", isHtml, DbType.Boolean);
            this.dataHelper.AddParameter("Title", title, DbType.String);
            this.dataHelper.AddParameter("ToMails", toMails, DbType.String);
            this.dataHelper.ExecuteWithoutResult();
        }

        public override void InvokeAutomaticStatusChange() {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_ActionMailActions";
            this.dataHelper.ExecuteWithoutResult();

            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_ActionConditionals";
            this.dataHelper.ExecuteWithoutResult();
        }

        public override void DeleteInterruptedReservations() {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_WebReservationDeleteInterrupted";
            this.dataHelper.ExecuteWithoutResult();
        }

        public override void MarkAsActive(string stockActionId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_ActionMarkAsActive";
            this.dataHelper.AddParameter("StockActionId", stockActionId, DbType.String);
            this.dataHelper.ExecuteWithoutResult();
        }

        public override void SaveLog(int logId, DateTime stardDate, DateTime endDate, string status, string title, string message) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_ActionLogSave";
            this.dataHelper.AddParameter("LogId", logId, DbType.Int32);
            this.dataHelper.AddParameter("StartDate", stardDate, DbType.DateTime);
            this.dataHelper.AddParameter("EndDate", endDate, DbType.DateTime);
            this.dataHelper.AddParameter("Status", status, DbType.String);
            this.dataHelper.AddParameter("Title", title, DbType.String);
            this.dataHelper.AddParameter("Message", message, DbType.String);
            this.dataHelper.ExecuteWithoutResult();
        }

        protected override IDataReader GetReaderForDequeueTopAction(string language) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_ActionDequeue";
			this.dataHelper.AddParameter("Language", language, DbType.StringFixedLength, 5);
            return this.dataHelper.Execute();
        }

        #endregion

    }
}
